//
//  BaseTabBarViewController.m
//  预购清单
//
//  Created by  on 2022/10/15.
//
#import "UITabBar+btn.h"

#import "BaseTabBarViewController.h"
#import "MineViewController.h"
#import "HomeViewController.h"
#import "FavoriteViewController.h"
//#import "BaseNavigationnViewController.h"

@interface BaseTabBarViewController ()

@end

@implementation BaseTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addMidbtn];
}
- (void)addMidbtn{
    CGFloat height = 50;
    UIView *bgbtn = [[UIView alloc] initWithFrame:CGRectMake((self.tabBar.frame.size.width - height)*0.5, -30, height, height)];
//    UIButton * shot = [[UIButton alloc] initWithFrame:CGRectMake((height - 35)*0.5, (height - 40)*0.5, 35, 35)];
        UIButton * shot = [[UIButton alloc] initWithFrame:CGRectMake((self.tabBar.frame.size.width - height)*0.5, -15, height, height)];
    shot.cornerRadius = 25;
    [shot addTarget:self action:@selector(showMidController) forControlEvents:UIControlEventTouchUpInside];
    [shot setImage:[UIImage imageNamed:@"数据"] forState:UIControlStateNormal];
    bgbtn.bgHexColor = @"#2756CA";
    bgbtn.cornerRadius = 30;
//    [bgbtn addSubview:shot];
    self.tabBar.btn = shot;
    [self.tabBar addSubview:shot];
}
- (void)showMidController{
    self.selectedIndex = 1;
}
- (void)showFirstController{
    FavoriteViewController *vc = [FavoriteViewController new];
    [[self currentVC] push:vc :NO];
}
- (void)setupColor{
    //    self.tabBar.translucent = NO;
    self.tabBar.tintColor = [@"#0041C4" color];
    self.tabBar.unselectedItemTintColor = [@"#000000" color];
    self.tabBar.backgroundImage = [UIImage withColor:Hex(@"#ECEFF6")];
    self.tabBar.backgroundColor = Hex(@"#ECEFF6");
    
    CGRect bounds = CGRectMake(0, 0, SCREEN_WIDTH, self.tabBar.bounds.size.height + 40);
    
    UIBezierPath * path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 12)];
    [path addArcWithCenter:CGPointMake(12, 12) radius:12 startAngle:M_PI endAngle:M_PI*3/2 clockwise:YES];
    [path addArcWithCenter:CGPointMake((SCREEN_WIDTH - 50 - 13) / 2 - 10, 12) radius:12 startAngle:M_PI*3/2 endAngle:0 clockwise:YES];
    [path addArcWithCenter:CGPointMake(SCREEN_WIDTH/ 2,10) radius:(50 + 13) / 2 - 2 startAngle:M_PI - 0.092 * M_PI endAngle:0.092 * M_PI clockwise:NO];
    [path addArcWithCenter:CGPointMake((SCREEN_WIDTH + 50 + 13) / 2 + 10, 12) radius:12 startAngle:M_PI endAngle:M_PI*3/2 clockwise:YES];
    [path addLineToPoint:CGPointMake(SCREEN_WIDTH - 12, 0)];
    [path addArcWithCenter:CGPointMake(SCREEN_WIDTH - 12, 12) radius:12 startAngle:M_PI*3/2 endAngle:0 clockwise:YES];
    [path addLineToPoint:CGPointMake(SCREEN_WIDTH, self.tabBar.bounds.size.height + 40)];
    [path addLineToPoint:CGPointMake(0, self.tabBar.bounds.size.height + 40)];
    [path addLineToPoint:CGPointMake(0, 12)];


    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = bounds;
    layer.path = path.CGPath;
    layer.fillColor = Hex(@"#FFFFFF").CGColor;
    [self.tabBar.layer addSublayer:layer];
    
}
- (void)setupViewController{
    
    NSArray *controllerArray = @[[UIViewController new],[HomeViewController new],[MineViewController new]];
    NSArray *titles = @[@"",@"",@""];
    NSArray *imgs = @[@"收藏",@"",@"我的"];
    NSArray *selectimgs = @[@"",@"",@""];
    for (int i = 0; i < controllerArray.count; i++) {
        UIViewController *vc = controllerArray[i];
        vc.tabBarItem = [[UITabBarItem alloc] init];
        vc.tabBarItem.title = LS(titles[i]);
        vc.tabBarItem.image = [[UIImage imageNamed:imgs[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectimgs[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc.tabBarItem.enabled = i != 1;
        CCNavigationController *nav = [[CCNavigationController alloc] initWithRootViewController:vc];
        [self addChildViewController:nav];
    }
    self.selectedIndex = 1;
}
#pragma mark - UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    CCNavigationController*navc = (CCNavigationController*)viewController;
   NSString *name = NSStringFromClass([navc.visibleViewController class]);
    if ([name isEqualToString:@"UIViewController"]) {
        [self showFirstController];
        return NO;
    }
    return YES;
}

@end
